package com.zhao.reggie3.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhao.reggie3.common.BaseContext;
import com.zhao.reggie3.common.R;
import com.zhao.reggie3.damain.ShoppingCart;
import com.zhao.reggie3.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description:
 * @author: zhao
 * @time: 2022/8/22 16:34
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * @param
     * @return
     * @description 添加购物车
     **/
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //本次添加的菜品id
        Long dishId = shoppingCart.getDishId();
        //本次添加的套餐id
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            //购物车是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //购物车是菜品
            lqw.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(lqw);

        if (one != null) {
            //如果已经存在，就在原来数量基础上加一
            //获取当前菜品或者套餐数量
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);

    }

    /**
     * @param
     * @return
     * @description 查询购物车
     **/
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //从ThreadLocal中获取用户id
        Long currentId = BaseContext.getCurrentId();
        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //根据用户id 每个用户的购物车数据不同
        lqw.eq(ShoppingCart::getUserId, currentId);
        //根据创建时间降序
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        //查询购物车表 返回购物车数据集合
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * @param
     * @return
     * @description 清空购物车
     **/
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清空成功");
    }
}